package cn.lds.j368.dsbank.domain;

import com.baomidou.mybatisplus.annotation.*;

import java.util.Collection;
import java.util.Date;
import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * <p>
 * 职员表
 * </p>
 *
 * @author Christopher
 * @since 2023-04-04
 */
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("t_employee")
public class Employee implements UserDetails,Serializable {

    private static final long serialVersionUID = 1L;

      @TableId(value = "EMP_ID", type = IdType.AUTO)
    private Integer empId;

    private String empName;

    private String empUsername;

    private String empPassword;

    private String empTelNo;

    private Date createTime;

    private Date modifyTime;

    private Integer createEmpId;

    private Integer modifyEmpId;

    @TableLogic
    private Integer isDeleted;

    @TableField(exist = false)
    private Employee createEmp;

    @TableField(exist = false)
    private Employee modifyEmp;




    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {

        List<SimpleGrantedAuthority> simpleGrantedAuthorities = this.roles
                .stream()
                .map(role -> role.getAuthorityList())
                .findAny()
                .get()
                .stream()
                .map(authority -> new SimpleGrantedAuthority("ROLE_" + authority.getAuthorityName() ))
                .collect(Collectors.toList());

        return simpleGrantedAuthorities;
    }

    @Override
    public String getPassword() {
        return this.empPassword;
    }

    @Override
    public String getUsername() {
        return this.getEmpUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.isDeleted == 0 ? true : false;
    }

    @TableField(exist = false)
    private List<Role> roles;
}
